using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections.Specialized;

namespace Axe.Logging
{
    public class TextFileLogger : ILogger
    {
        string m_LogFileName;

        #region ILogger Members

        public void Add(LogEntry entry)
        {
            if (entry == null)
                return;

            using (StreamWriter sw = new StreamWriter(m_LogFileName, true, Encoding.UTF8))
            {
                sw.WriteLine(entry.ToString());
                sw.Flush();
            }           
        }

        #endregion

        public TextFileLogger()
        {

        }

        public TextFileLogger(string logFileName)
        {
            m_LogFileName = logFileName;
        }



        #region ILogger Members

        public bool Initialize(NameValueCollection cfg)
        {
            m_LogFileName = (string)cfg["filePath"];

            string logPath = Path.GetDirectoryName(m_LogFileName);

            if(string.IsNullOrEmpty(logPath) == false)
            {
                FileSystem.EnsureDirectory(logPath);
            }

            return true;
        }

        #endregion
    }
}
